package com.weiguan.xmpp;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smackx.muc.MultiUserChat;
import org.jivesoftware.smackx.muc.MultiUserChatManager;

import com.alibaba.fastjson.JSONObject;
import com.weiguan.xmpp.util.FileUtil;
import com.weiguan.xmpp.util.XMPPTCPConnectionUtils;

public class Test3 {

	private static final int AddCount = 100;// 加入的个数
	private static final int TIME_PEIOD = 3000;// 进入间隔

	private static List<String> Name = new ArrayList<String>();

	private static int RoomId = 0;
	private static int BeginUserId = 100000200;// 起始id

	private static void initRoom(int roomId, int beginUserid) {
		Test3.RoomId = roomId;
		Test3.BeginUserId = beginUserid;
	}

	public static void main(String[] args) throws InterruptedException {

		initRoom(Config.ROOM_ID, BeginUserId);

		Set<String> names = FileUtil.readFileName(new File("D://name3.txt"));

		if (names == null || names.size() < 100) {
			System.out.println("读取名字错误");
			return;
		}
		Name = new ArrayList<String>(names);
		System.out.println(Name.size());
		TestCreateUser();

		Scanner s = new Scanner(System.in);
		while (true) {
			String y = s.nextLine();
			if (y.equals("out")) {
				break;
			}
		}

		TestDeleteUser();
	}

	private static void TestCreateUser() throws InterruptedException {
		for (int userId = BeginUserId; userId < (BeginUserId + AddCount); userId++) {
			createUserAndIntoRoom(userId, Name.remove(0), RoomId);
			Thread.sleep(TIME_PEIOD);
		}
	}

	private static void TestDeleteUser() {
		for (int userId = BeginUserId; userId < BeginUserId + AddCount; userId++) {
			deleteUser(userId);
		}
	}

	private static MultiUserChat joinMultiUserChat(String userId, String userName, String roomsName,
			XMPPConnection connection) {
		try {
			// 使用XMPPConnection创建一个MultiUserChat窗口
			MultiUserChat muc = MultiUserChatManager.getInstanceFor(connection)
					.getMultiUserChat(roomsName + "@conference." + connection.getServiceName());

			// 用户加入聊天室
			// muc.join(userId + ":" + userName + ":0");
			muc.join(userId + ":" + userName);

			//String s = "1%!%101%!%fromUserAlias%!%%!%fromUserTrId^^^^^fromUserAlias^1^fromUserId^^^7^fromUserPic^fromUserAlias";
			// 公聊
			// s = "1%!%100%!%" +userName+ "%!%%!%" +userId+
			// "^"+roomsName+"^天平^来来来";

			//muc.sendMessage(s);
			System.out.println(userId + " 加入会议室成功........");
			return muc;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(userId + " 加入会议室失败........");
			return null;
		}
	}

	private static void joinToRoom(String userId, String password, String userName, String roomName) {
		XMPPTCPConnection con = XMPPTCPConnectionUtils.getXMPPTCPConnection(Config.getServiceName(), Config.getHost(),
				userId, password);

		if (con != null) {
			// conList.add(con);
			joinMultiUserChat(userId, userName, roomName, con);
		} else {
			System.out.println(userId + " 登陆失败!");
		}

	}

	// 返回201创建成功,其余的失败,409用户已存在
	private static void createUserAndIntoRoom(int userId, String userName, int roomId) {
		String createUserURL = "http://%s:9090/plugins/restapi/v1/users";

		createUserURL = String.format(createUserURL, Config.getXmppURL());

		JSONObject obj = new JSONObject();
		obj.put("username", String.valueOf(userId));
		obj.put("password", String.valueOf(userId));

		int code = Config.HttpGoReturnStatus(createUserURL, "POST", obj.toString());

		if (code == 201) {
			System.out.println("用户创建成功!userId=" + userId);
		} else if (code == 409) {
			System.out.println("用户已存在!userId=" + userId);
		}

		if (code == 201 || code == 409) {
			// 加入房间
			joinToRoom(String.valueOf(userId), String.valueOf(userId), String.valueOf(userName),
					String.valueOf(roomId));
		}
	}

	// 返回200删除成功,其余的失败,404用户不存在
	private static void deleteUser(int userId) {
		String deleteUserURL = "http://%s:9090/plugins/restapi/v1/users/%s";

		deleteUserURL = String.format(deleteUserURL, Config.getXmppURL(), String.valueOf(userId));

		int code = Config.HttpGoReturnStatus(deleteUserURL, "DELETE", null);

		if (code == 200) {
			System.out.println("用户删除成功!userId=" + userId);
		} else if (code == 404) {
			System.out.println("用户不存在!userId=" + userId);
		}
	}
}
